home *** CD-ROM | disk | FTP | other *** search
-
- /* CONSOLE Window Handler
- Copyright (C) 1993-1994 G.Woigk
-
- This file is part of the ZX LOADER program and it is free software
- See Application.c for details
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-
- 22.11.93: Start of work on this file
- 28.6.94: Check for 'Console open' modified
- 1.12.94: Bilingual enhancement added (switch is in application.h)
- */
-
- #include "kio.h"
- #include "mem.h"
- #include "application.h"
- #include "eventloop.h"
- #include "console.h"
-
- // With any call of any function the console window is opened automatically
- // line[] is used for window refresh
- #define maxLines 40
- #define border 2 // border between text and window border
- #define COFF if((--crsr)==0)Crsr()
- #define CON if((crsr++)==0)Crsr()
- #define COLD if(crsr>0)Crsr()
- #define titel "\pLogbuch"
- #define font "\pMonaco"
- #define fontSize 9
- #define tabWidth 8
- #define conWindowID 128 // default 'WIND' resourceID
-
- short fontHeight; // line spacing := size*4/3 will be calculated
- short fontWidth; // medium character width will be calculated
- WindowPtr conWindow = nil;
- short col,row; // cursor location (character coordinates)
- short cols,rows; // text columns & rows fitting in the window
- Rect box; // the windows contents rectangle in local coordinates
- short crsr = 0; // is the cursor blob displayed? (yes, if crsr==1)
- Str255 line[maxLines+1]; // used for refresh
-
-
- // ----- prototypes ---------------------------------------------------------
- BBox ();
- ScrollScreen ();
- Crsr();
-
-
- // ----- calculate bounding rectangle of console window in local coord. -----
- BBox () // test for scroll bars needed ?
- {
- memcpy ( &box, &conWindow->portRect, 8);
- box.bottom -= (box.top+border+border);
- box.right -= (box.left+border+border);
- box.left=0;
- box.top=0;
- rows=box.bottom/fontHeight; if (rows>=maxLines) rows=maxLines-1;
- cols=box.right/fontWidth; if (cols>255) cols=255;
- SetOrigin(-border,-border);
- }
-
- // ----- scroll screen up one line --------------------------
- ScrollScreen ()
- { RgnHandle h,u;
- h = NewRgn();
- u = NewRgn();
- ScrollRect ( &box, 0, -fontHeight, h ); // scroll up
- OffsetRgn ( ((WindowRecord*)conWindow)->updateRgn, 0, -fontHeight );
- box.top=box.bottom-fontHeight; // freshly unhidden line
- RectRgn ( u, &box); // convert to Rgn
- DiffRgn ( h,u,h ); // subtract new line from revealed Rgn
- if (!EmptyRgn(h)) InvalRgn(h); // something hidden revealed => refresh!
- DisposeRgn(h);
- DisposeRgn(u);
- memmove ( line[0], line[1], sizeof(Str255)*(maxLines-1) );
- line[maxLines-1][0]=0;
- box.top=0;
- }
-
- // ----- show/hide cursor blob -------------------------------
- Crsr() // UP: show / hide blob depending on crsr-flag
- { Point p;
- Char c;
- if (crsr) TextMode(notSrcCopy);
- p=conWindow->pnLoc;
- if (col>=line[row][0]) c=' '; else c=line[row][col+1];
- DrawChar(c);
- conWindow->pnLoc=p;
- if (crsr) TextMode(srcCopy);
- }
-
- ConCrsr(Boolean f)
- {
- if ( (crsr==1)==f ) return;
- crsr=f;
- if (!conWindow) return;
- SetPort(conWindow);
- COLD;
- }
-
- // ----- Clear/Open Console Window ---------------------------
- ConCLS()
- { short i;
- if (!conWindow)
- {
- if (conWindowID) conWindow = GetNewWindow (conWindowID,nil,(WindowPtr)-1);
- if (!conWindow)
- {
- box.left = 20;
- box.top = 50;
- box.right = box.left + 400;
- box.bottom= box.top + 200;
- conWindow = NewWindow (nil, &box, titel, TRUE, 0, (WindowPtr)-1, TRUE, 0);
- if (!conWindow) DoAbort("\pCould not create console window","\p","\p");
- }
- }
-
- SetPort ( conWindow );
- ShowWindow ( conWindow );
- SelectWindow ( conWindow );
- GetFNum(font,&i);
- TextFont(i);
- TextMode(srcCopy);
- TextSize(fontSize);
- fontHeight=fontSize*4/3;
- fontWidth=CharWidth('e');
- for (i=0; i<maxLines; i++) line[i][0]=0;
- BBox();
- EraseRect(&box);
- col=0;row=0;
- MoveTo(0,fontSize);
- COLD;
- }
-
- // ----- Close Console Window --------------------------------
- ConClose ()
- {
- if (conWindow) DisposeWindow ( conWindow );
- conWindow=nil;
- }
-
- // ----- move cursor to begin of next line ----- may scroll ---------
- ConNL ()
- {
- if (!conWindow) ConCLS();
- SetPort ( conWindow );
- COFF;
- if ( row+1>=rows ) ScrollScreen();
- else Move ( 0,fontHeight ), row++;
- conWindow->pnLoc.h=0;
- col=0;
- CON;
- }
-
- // ----- print a character ----- no control codes ------ may scroll ---------
- ConChar ( Char c )
- { Char z;
- Rect b;
- if (!conWindow) ConCLS();
- SetPort ( conWindow );
- COFF;
- if ( (col)>=cols ) ConNL();
- DrawChar(c);col++;
- z=line[row][col];
- line[row][col]=c;
- if (col>line[row][0]) line[row][0]=col;
- else if (CharWidth(c)!=CharWidth(z))
- { b.left = conWindow->pnLoc.h;
- b.top = fontHeight*row;
- b.bottom= b.top+fontHeight;
- b.right = box.right;
- InvalRect(&b);
- }
- CON;
- }
-
- // ----- print a string ----- no control codes ----- may scroll ------------
- ConPrint ( Str255 eng, Str255 ger )
- { Rect b;
- Char *s;
- if (!conWindow) ConCLS();
- SetPort (conWindow);
- #if german
- if (ger) s=ger; else s=eng;
- #else
- if (eng) s=eng; else s=ger;
- #endif
-
- if (col+s[0]>cols)
- { short n;
- Str255 ss;
- n=s[0]-(cols-col); // characters in next line
- ss[0]=cols-col; // characters in this line
- memcpy ( ss+1, s+1, ss[0] );
- ConPrint(ss,0);
- ConNL();
- memcpy ( ss+1, s+1+ss[0], n );
- ss[0]=n;
- ConPrint(ss,0);
- return;
- }
-
- COFF;
- DrawString(s);
- memcpy ( line[row]+col+1, s+1, s[0] );
- col=col+s[0];
- if (col>line[row][0]) line[row][0]=col;
- else
- { b.left = conWindow->pnLoc.h;
- b.top = fontHeight*row;
- b.bottom= b.top+fontHeight;
- b.right = box.right;
- InvalRect(&b);
- }
- CON;
- }
-
- // ----- move cursor LEFT ------ stop at left border ------------------------
- ConLeft()
- { char c;
- if ( (!conWindow) || (col==0) ) return;
- SetPort (conWindow);
- COFF;
- Move( -CharWidth(line[row][col--]),0 );
- CON;
- }
-
- // ----- move cursor RIGHT ----- stop at right border -----------------------
- ConRight()
- {
- if ( (!conWindow) || col>=cols ) return;
- SetPort (conWindow);
- COFF;
- if (col>=line[row][0]) ConChar(' ');
- else Move ( CharWidth(line[row][++col]),0 );
- CON;
- }
-
- // ----- move cursor to next TAB position -----------------------------------
- ConTab()
- {
- ConHLocate ( (col+tabWidth)/tabWidth*tabWidth );
- }
-
- // ----- locate cursor HORIZONTALLY -----------------------------------------
- ConHLocate(short h)
- { short n;
- if (!conWindow) return;
- SetPort (conWindow);
- COFF;
- n=line[row][0];
- if (h>n)
- { ConHLocate(n);
- if (h>cols) h=cols;
- for ( h-=n; h; h-- ) ConChar(' ');
- }
- else
- { if (h<0) h=0;
- line[row][0]=h;
- MoveTo ( StringWidth(line[row]), conWindow->pnLoc.v );
- line[row][0]=n;
- col=h;
- }
- CON;
- }
-
- // ----- locate cursor ------------------------------------------------------
- ConLocate (short z, short s)
- {
- if (!conWindow) return;
- SetPort (conWindow);
- COFF;
- if (z<0) row=0; else if (z>=rows) row=rows-1; else row=z;
- MoveTo ( 0, fontSize+row*fontHeight );
- col=0; ConHLocate (s);
- CON;
- }
-
- // ----- clear to end of line -----------------------------------------------
- ConCLEOL()
- { Rect b;
- if (!conWindow) return;
- SetPort (conWindow);
- line[row][0]=col;
- b.left = conWindow->pnLoc.h;
- b.top = fontHeight*row;
- b.bottom= b.top+fontHeight;
- b.right = box.right;
- EraseRect(&b);
- COLD;
- }
-
- // ----- refresh screen -----------------------------------------------------
- ConRfsh ()
- { Point p;
- int i;
- if (!conWindow) return; // this should never happen
- SetPort ( conWindow );
- BBox(); // recalculate contents box
- EraseRect(&box);
- if (row>=rows) {}; // more action needed after resize window ...
- p = conWindow->pnLoc;
- MoveTo(0,fontSize);
- for ( i=0; i<rows; i++ )
- {
- DrawString(line[i]);
- conWindow->pnLoc.h=0;
- conWindow->pnLoc.v += fontHeight;
- }
- conWindow->pnLoc = p;
- COLD;
- }
-
- // ----- Print string and move cursor to begin of next line ------------------------
- ConMsg(Str255 eng, Str255 ger)
- { ConPrint(eng,ger);
- ConNL();
- }
-
-
-